package com.sinat.flowable.service.impl;


import com.sinat.flowable.mapper.FlowableCommonMapper;
import com.sinat.flowable.service.ProcessInstanceService;
import com.sinat.flowable.vo.CategoryVo;
import com.sinat.flowable.vo.ProcessDefinitionVo;
import com.sinat.flowable.vo.ProcessInstanceQueryVo;
import com.sinat.flowable.vo.ProcessInstanceRequest;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 庄金明
 * @date 2020年3月23日
 */
@Service
public class ProcessInstanceServiceImpl implements ProcessInstanceService {
    @Resource
    private FlowableCommonMapper flowableCommonMapper;

    @Override
    public ProcessInstance getProcessInstanceById(String processInstanceId) {
        return null;
    }

    @Override
    public HistoricProcessInstance getHistoricProcessInstanceById(String processInstanceId) {
        return null;
    }

    @Override
    public void start(ProcessInstanceRequest processInstanceRequest) {

    }

    @Override
    public void delete(String processInstanceId, boolean cascade, String deleteReason) {

    }

    @Override
    public void activate(String processInstanceId) {

    }

    @Override
    public void suspend(String processInstanceId) {

    }

    @Override
    public List listMyInvolvedSummary(ProcessInstanceQueryVo processInstanceQueryVo) {
        List<ProcessDefinitionVo> vos = flowableCommonMapper.listMyInvolvedSummary(processInstanceQueryVo);
        List<CategoryVo> result = new ArrayList<>();
        Map<String, List<ProcessDefinitionVo>> categorysByParent = new HashMap<>();
        for (ProcessDefinitionVo vo : vos) {
            List<ProcessDefinitionVo> childs = categorysByParent.computeIfAbsent(vo.getCategory(),
                    k -> new ArrayList<>());
            childs.add(vo);
        }
        for (Map.Entry<String, List<ProcessDefinitionVo>> entry : categorysByParent.entrySet()) {
            CategoryVo aCategoryVo = new CategoryVo();
            aCategoryVo.setCategory(entry.getKey());
            aCategoryVo.setProcessDefinitionVoList(entry.getValue());
            String categoryName = entry.getValue().iterator().next().getCategoryName();
            aCategoryVo.setCategoryName(categoryName);
            result.add(aCategoryVo);
        }
        return result;
    }
}
